<?php

declare(strict_types=1);

namespace app\common\service\im;

use app\common\traits\ServiceTrait;
use app\common\dao\im\GroupUserDao;
use app\common\dao\sys\UserDao;

/**
 * 聊天群用户关联 - 业务
 * @author:kaige
 * @update:2022-06-13
 * @copyright ©2022 泉州市盛筑信息科技有限公司 版权所有
 */
class GroupUserService
{
  protected $dao = null;

  public function __construct()
  {
    $this->dao = new GroupUserDao();
  }
  use ServiceTrait;

  /**
   * 重写 关联 with
   */
  public function _list_with()
  {
    return ['glgroup', 'gluser'];
  }

  /**
   * 重写 只显示关联的字段
   */
  public function _list_visible()
  {
    return ['gluser' => ['id', 'un', 'real_name', 'dept_id_name', 'ranking_id_name', 'avatar']];
  }

  /**
   * 重写 列表过滤条件
   * @param $post
   * @return array
   */
  public function _list_where($post)
  {
    $where = [];
    if (isset($post['group_id']) && $post['group_id'] != "") {
      $where[] = ['group_id', '=', $post['group_id']];
    }
    if (isset($post['uid']) && $post['uid'] != "") {
      $where[] = ['uid', '=', $post['uid']];
    }
    if (isset($post['status']) && $post['status'] != "") {
      $where[] = ['status', '=', $post['status']];
    }

    $result['where'] = $where;
    $result['field'] = [];
    $result['order'] = ['is_top desc'];
    return $result;
  }

  /**
   * 重写 数据格式化
   */
  public function _list_format($list)
  {
    foreach ($list as $k => &$v) {
      if (isset($v['chat_bg'])) {
        $list[$k]['chat_bg'] = _img($v['chat_bg']);
      }
      if (isset($v['gluser']['avatar'])) {
        $list[$k]['avatar'] = _img($v['gluser']['avatar']);
      }
    }
    return $list;
  }

  /**
   * 获取用户加入的所有群 - 接口
   */
  public function getListByUserId($id = 0)
  {
    return $this->dao->getListByUserId($id);
  }

  /**
   * 根据群ID获取群所有成员 - 接口
   */
  public function getImGroupUserList($id = 0)
  {
    return $this->dao->getImGroupUserList($id);
  }

  /**
   * 根据群ID获取群所有成员
   */
  public function getImGroupUserListSzim($id = 0)
  {
    $list = $this->dao->getImGroupUserListLayim($id);
    if ($list) {
      foreach ($list as $k => &$v) {
        if (isset($v['gluser']['real_name'])) {
          $list[$k]['username'] = $v['gluser']['real_name'];
        }
        if (isset($v['gluser']['avatar'])) {
          $list[$k]['avatar'] = _img($v['gluser']['avatar']);
        }
      }
    }
    return $list;
  }

  /**
   * 根据群ID获取群所有成员ID - 接口
   */
  public function getImGroupUserIds($id = 0)
  {
    return $this->dao->getImGroupUserIds($id);
  }

  /**
   * 添加、编辑数据保存
   * @param $data
   * @return array
   */
  public function saveData($data)
  {
    url_decode($data); // 前端已 encodeURIComponent 编码（最深二维数组） 后端需要解码
    $this->formatData($data);
    return $this->saveForm($data);
  }
  /**
   * APP批量添加群成员方法
   * @return array
   */
  public function addAllGroupUser($data)
  {
    $this->formatData($data);
    $result = [
      'status' => false,
      'data'   => '',
      'msg'    => ''
    ];
    url_decode($data); // 前端已 encodeURIComponent 编码（最深二维数组） 后端需要解码
    //返回数组元素组合成的字符串
    $ids = implode(',', parse_ugdc($data['uid']));
    //字符串组成的数组
    $idsarr = explode(',', $ids);
    foreach ($idsarr as $k => $v) {
      $where = [];
      $where['id'] = $v;
      $user = (new UserDao)->getInfoByWhere($where);
      $data1['group_id'] = $data['group_id'];
      $data1['uid'] = $v;
      $data1['nickname'] = $user['real_name'];
      $data1['is_top'] = $data['is_top'];
      $this->dao->add($data1);
    }
    return _ok('批量添加成员成功');
  }
  /**
   * AP批量删除群成员
   * @return array
   */
   public function delAllGroupUser($data){
    $result = [
      'status' => false,
      'data'   => '',
      'msg'    => ''
    ];
    url_decode($data); // 前端已 encodeURIComponent 编码（最深二维数组） 后端需要解码
    //返回数组元素组合成的字符串
    $ids = implode(',', parse_ugdc($data['uid']));
    //字符串组成的数组
    $idsarr = explode(',', $ids);
    foreach ($idsarr as $k => $v) {
      $where = [];
      $where['uid'] = $v;
      $where['group_id'] = $data['group_id'];
      $this->dao->deleteByWhere($where);
    }
    return _ok('批量删除成员成功'); 
   }
  /**
   * 格式化存储的数据--存储
   * @return void
   */
  protected function formatData(&$data)
  {
    $data['is_show_nick'] = checkon('is_show_nick', $data);
  }


  public function getUserQunList($user_id = 0, $field = ['*'])
  {
    return $this->dao->getListByUserId($user_id, $field);
  }
}
